<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$petId = $_POST["pet_id"];
$predicateDate= $_POST["predicate_date"];
$predicateDetail = $_POST["predicate_detail"];
$maintenanceDetail = $_POST["maintenance_detail"];
$maintenanceDate = $_POST["maintenance_date"];
$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "predicate"){
	$predicateData = $_POST["predicate_data"];
	$arrData["pet_id"] ="".$petId."";
	$arrData["history_type"] ="P";
	$arrData["predicate_detail"] ="".$predicateDetail."";
	$arrData["predicate_data"] = "".$predicateData."";
	$arrData["price"] ="".$price."";
	$arrData["predicate_date"] ="".$predicateDate."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "history");
	header("Location:../contents/maintenance_form.php?mode=maintenance&pet_id=".$petId);
}else if($mode == "maintenance"){
	$medicalCareData = $_POST["medical_care_data"];
	$medicineData = $_POST["medicine_data"];
	$medicinePrice = $_POST["medicine_price"];
	$medicalCarePrice = $_POST["medical_care_price"];
	$sumPrice = $medicalCarePrice + $medicinePrice;
	$arrData["pet_id"] ="".$petId."";
	$arrData["history_type"] ="M";
	$arrData["medical_care_data"] ="".$medicalCareData."";
	$arrData["maintenance_date"] = "".$maintenanceDate."";
	$arrData["medicine_data"] = "".$medicineData."";
	$arrData["price"] ="".$sumPrice."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "history");
	//insert history medicine
	$myMedicaineData = $_POST["medicine_data"];
	$explodeData = explode(",", $myMedicaineData);
	for($i=0;$i<sizeof($explodeData);$i++){
		$buffer =  $explodeData[$i];
		$myExplodeData = explode(":", $buffer);
		$priceData = $managerIUDS->queryData("price", "medicine", " WHERE medicine_id = '".$myExplodeData[0]."'", "1", "");
		$amount = $myExplodeData[1];
		$medicineArrData = explode("|",$_POST["medicine_detail_data"][$i]);
		$price = $amount * $priceData["price"];
		$arrHisData["medicine_id"] = "".$myExplodeData[0]."";
		$arrHisData["history_type"] = "E";
		$arrHisData["amount"] = "".$amount."";
		$arrHisData["price"] = "".$price."";
		$managerIUDS->inserData($arrHisData, "history_medicine");
	}
	//updata medicine table 
	for($i=0;$i<sizeof($explodeData);$i++){
		$buffer =  $explodeData[$i];
		$myExplodeData = explode(":", $buffer);
		$amounData = $managerIUDS->queryData("amount", "medicine", " WHERE medicine_id = '".$myExplodeData[0]."'", "1", "");//get amount by id
		$medicineAmount= $amounData["amount"] - $myExplodeData[1] ;
		$arrPrimaryKey["medicine_id"] = "".$myExplodeData[0]."";
		$arrMedicineData["amount"] = "".$medicineAmount."";
		$arrMedicineData["update_date"] = "".date("Y-m-d")."";
		$arrMedicineData["update_by"] = "".$_SESSION["user_name"]."";
		$managerIUDS->updateData($arrMedicineData, "medicine", $arrPrimaryKey, "");
	}
	$historyMedicalCareData = $managerIUDS->queryData("MAX(history_id) as max_id,pet_id", "history", " WHERE history_type='M'", "1", "");
	$historyPredicateData = $managerIUDS->queryData("MAX(history_id) as max_id", "history", " WHERE history_type='P'", "1", "");
	header("Location:../contents/show_report.php?medical_care_id=".$historyMedicalCareData["max_id"]."&pet_id=".$historyMedicalCareData["pet_id"]."&maintenance_date=".$maintenanceDate."&predicate_id=".$historyPredicateData["max_id"]);
}else if($mode == "searchPetByName"){
		$petName = $_GET["pet_name"];
		header("Location:../contents/predicate_history_form.php?mode=search&page=".$page."&pet_name=".urlencode($petName));
}else if($mode == "searchPetByNameModeMaintenance"){
		$petName = $_GET["pet_name"];
		header("Location:../contents/maintenance_history_form.php?mode=search&page=".$page."&pet_name=".urlencode($petName));
}
?>